package com.jic.market.impl.rest.discount.processor;

import com.jic.common.utils.IDUtils;
import com.jic.market.enums.DiscountAmountServiceOperateEnum;
import com.jic.market.enums.DiscountDetailStatusEnum;
import com.jic.market.impl.rest.discount.model.GoodsDetailModel;
import com.jic.market.request.rest.discount.DiscountBaseRequest;
import com.jic.market.request.rest.discount.DiscountOrderPayRequest;
import lombok.Data;

import java.math.BigDecimal;

/**
 * @author yinhaijun
 * @date: 2020/5/6
 */
public abstract class AbstractSingleDiscountProcessor extends BaseDiscountProcessor {

    public void queryShoppingCart(GoodsDetailModel goodsDetailModel, DiscountBaseRequest request) {
        SingleResult singleResult = isCondition(goodsDetailModel, request, DiscountAmountServiceOperateEnum.SETTLEMENT_QUERY);
        goodsDetailModel.setSingleMessage(singleResult.getMessage());
        // 未通过处理
        if(!singleResult.getPass()){
            goodsDetailModel.setPromotionDiscountTotalAmount(BigDecimal.ZERO);
            goodsDetailModel.setPromotionDiscountFreightAmount(BigDecimal.ZERO);
        } else {
            queryShoppingCartAfter(singleResult, goodsDetailModel, request);
        }
    }

    public void querySettlement(GoodsDetailModel goodsDetailModel, DiscountBaseRequest request) {
        SingleResult singleResult = isCondition(goodsDetailModel, request, DiscountAmountServiceOperateEnum.SETTLEMENT_QUERY);
        goodsDetailModel.setSingleMessage(singleResult.getMessage());
        // 未通过处理
        if(!singleResult.getPass()){
            goodsDetailModel.setPromotionDiscountTotalAmount(BigDecimal.ZERO);
            goodsDetailModel.setPromotionDiscountFreightAmount(BigDecimal.ZERO);
        } else {
            // 运费
            goodsDetailModel.calcuDiscountFreightAmount(goodsDetailModel.getParcelsFlag());
            querySettlementAfter(singleResult, goodsDetailModel, request);
        }
    }

    public void create(GoodsDetailModel goodsDetailModel, Object request, String thirdUserId) {
        // 流水号生成
        goodsDetailModel.setPromotionSerialNo(IDUtils.GenerateBillNo());
        goodsDetailModel.setDiscountDetailStatusEnum(DiscountDetailStatusEnum.CREATE);
        createAfter(goodsDetailModel, request, thirdUserId);
    }

    public void pay(GoodsDetailModel goodsDetailModel, DiscountOrderPayRequest request) {
        goodsDetailModel.setDiscountDetailStatusEnum(DiscountDetailStatusEnum.PAY);
        // 对应商品的状态更改
        payAfter(goodsDetailModel, request);
    }

    public void cancel(GoodsDetailModel goodsDetailModel, Object request) {
        goodsDetailModel.setDiscountDetailStatusEnum(DiscountDetailStatusEnum.REFUND);
        cancelAfter(goodsDetailModel, request);
    }

    /** 是否符合优惠条件 */
    public abstract SingleResult isCondition(GoodsDetailModel goodsDetailModel, DiscountBaseRequest request, DiscountAmountServiceOperateEnum operate);

    protected void queryShoppingCartAfter(SingleResult singleResult, GoodsDetailModel goodsDetailModel, DiscountBaseRequest request) {
    }

    protected void querySettlementAfter(SingleResult singleResult, GoodsDetailModel goodsDetailModel, DiscountBaseRequest request){
    }

    protected void createAfter(GoodsDetailModel goodsDetailModel, Object request, String thirdUserId){
    }

    protected void payAfter(GoodsDetailModel goodsDetailModel, DiscountOrderPayRequest request){

    }

    protected void cancelAfter(GoodsDetailModel goodsDetailModel, Object request){

    }

    @Data
    public static class SingleResult{
        private Boolean pass;
        private String message;
        // 用于判断总额
        private BigDecimal conditionTotalAmount;
        // 用于判断数量
        private BigDecimal conditionNum;
        private Object object;
        public SingleResult(Boolean pass, String message){
            this.pass = pass;
            this.message = message;
        }
    }
}
